<?php
namespace App\Controllers\Admin;

use App\Controllers\AdminBase;
use App\Models\ArticleModel;

class Article extends AdminBase
{

    protected $articleModel;
    public $cats;

    public function __construct(...$param)
    {
        $this->articleModel = new ArticleModel();
        $this->cats = $this->articleModel->cat_get_select();
    }

    //管理列表
    public function index()
    {
        if (IS_POST) {
            $page = $this->request->getPost('page');
            $limit = $this->request->getPost('limit');
            list($list, $total, $param) = $this->articleModel->limit_page($page, $limit, 0);
            if ($list) {
                foreach ($list as $k => $v) {
                    $list[$k]['catname'] = $v['catid'] ? $this->cats[$v['catid']]['name'] : '';
                    $list[$k]['inputtime_data'] = date('Y-m-d H:i:s', $v['inputtime']);
                    $list[$k]['updatetime_data'] = date('Y-m-d H:i:s', $v['updatetime']);
                }
            }
            $return = [
                'total' => $total,
                'data' => $list,
            ];
            exit(ams_json(0, 'ok', $return));
        }
        $vdata = [
            'cats' => $this->cats,
        ];
        return view('article/index.html', $vdata);
    }

    //添加
    public function add()
    {
        if (IS_POST) {
            $data = $this->request->getPost('data');
            $rt = $this->articleModel->add($data);
            if ($rt['code'] == 0) {
                $this->addSystemLog('添加文章公告: ' . $data['title']);
                $this->_json(0, '操作成功');
            }
            $this->_json(1, $rt['msg']);
        }
        $vdata = [
            'cats' => $this->cats,
        ];
        return view('article/add.html', $vdata);
    }

    //修改
    public function edit()
    {
        $id = intval($this->request->getGet('id'));
        if (IS_POST) {
            $data = $this->request->getPost('data');
            $rt = $this->articleModel->edit($id, $data);
            if ($rt['code'] == 0) {
                $this->addSystemLog('修改文章公告: ' . $data['title']);
                $this->_json(0, '操作成功');
            }
            $this->_json(1, $rt['msg']);
        }
        $rt = $this->articleModel->get($id);
        $vdata = [
            'cats' => $this->cats,
            'data' => $rt['data'],
        ];
        return view('article/add.html', $vdata);
    }

    //删除
    public function del()
    {
        $yes = $no = 0;
        if (IS_POST) {
            $ids = $this->request->getPost('ids');
            ams_clean_xss($ids);
            if ($ids) {
                if (is_array($ids)) {
                    foreach ($ids as $id) {
                        $rt = $this->articleModel->del($id);
                        if ($rt['code'] == 0) {
                            $this->addSystemLog('删除文章:#' . $id);
                            $yes++;
                        } else {
                            $no++;
                        }
                    }
                } else {
                    $rt = $this->articleModel->del($ids);
                    if ($rt['code'] == 0) {
                        $this->addSystemLog('删除文章:#' . $ids);
                        $yes++;
                    } else {
                        $no++;
                    }
                }

            } else {
                $this->_json(1, '参数错误');
            }
            $this->_json(0, '操作结果 成功：' . $yes . ',失败' . $no);
        }
    }

    //设置状态
    public function status()
    {

    }

    //=======category======

    public function catindex()
    {
        $list = [];
        if (IS_POST) {
            $list = $this->articleModel->cat_get_select();
            $return = [
                'total' => count($list),
                'data' => $list,
            ];
            exit(ams_json(0, 'ok', $return));
        }
        $vdata = [];
        return view('article/catindex.html', $vdata);
    }

    public function catadd()
    {
        $pid = (int) $this->request->getGet('pid');
        $data = ['pid' => $pid];
        if (IS_POST) {
            $data = $this->request->getPost('data');
            $ret = $this->articleModel->cat_add($data);
            if ($ret['code'] == 0) {
                $this->addSystemLog('添加表[article]分类【#' . $data['name'] . '】'); // 记录日志
                $this->_json($ret['code'], $ret['msg'], $ret['data']);
            }
            $this->_json($ret['code'], $ret['msg'], $ret['data']);
        }
        $cats = $this->articleModel->cat_get_select();
        $vdata = [
            'data' => $data,
            'cats' => $cats,
        ];
        return view('article/catadd.html', $vdata);
    }

    public function catedit()
    {
        $id = (int) $this->request->getGet('id');
        $data = $this->articleModel->cat_get($id);
        if (IS_POST) {
            $data = $this->request->getPost('data');
            $ret = $this->articleModel->cat_edit($id, $data);
            if ($ret['code'] == 0) {
                $this->addSystemLog('修改表[article]分类【#' . $data['name'] . '】'); // 记录日志
            }
            $this->_json($ret['code'], $ret['msg'], $ret['data']);
        }
        $cats = $this->articleModel->cat_get_select();
        $vdata = [
            'data' => $data,
            'cats' => $cats,
        ];
        return view('article/catadd.html', $vdata);
    }

    public function catdel()
    {
        $yes = $no = 0;
        if (IS_POST) {
            $ids = $this->request->getPost('ids');
            if ($ids && is_array($ids)) {
                foreach ($ids as $id) {
                    $rt = $this->articleModel->cat_del($id);
                    if ($rt['code'] == 0) {
                        $this->addSystemLog('删除分类:#' . $id);
                        $yes++;
                    } else {
                        $no++;
                    }
                }
            } else {
                $this->_json(1, '参数错误');
            }
            $this->_json(0, '操作结果 成功：' . $yes . ',失败' . $no);
        }
        $this->_json(1, isset($rt['msg']) ? $rt['msg'] : '操作失败');
    }

    public function catcache()
    {
        $this->articleModel->cat_cache();
    }
}